package com.yhq.basic.jvm.dataarea;

import com.yhq.basic.jvm.dataarea.po.Person;

/**
 * ‐XX:+DoEscapeAnalysis :开启逃逸分析，默认开启
 * ‐XX:+EliminateAllocations：开启标量替换，默认开启
 * 案例：
 * 方式一：该AllotOnStack运行不会产生大量GC
 * ‐Xmx15m ‐Xms15m ‐XX:+DoEscapeAnalysis ‐XX:+PrintGC ‐XX:+EliminateAllocations
 * 方式二：
 * ‐Xmx15m ‐Xms15m ‐XX:-DoEscapeAnalysis ‐XX:+PrintGC ‐XX:+EliminateAllocations
 * 方式三：
 * ‐Xmx15m ‐Xms15m ‐XX:+DoEscapeAnalysis ‐XX:+PrintGC ‐XX:-EliminateAllocations
 * <p>
 * 总结：想要实现栈上分配，必须同时开启逃逸分析和标量替换
 *
 * @author hqyin
 * @date 2023/3/7 4:21 下午
 */
public class AllotOnStack {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            alloc();
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));

    }

    private static void alloc() {
        Person person = new Person();
        person.setAge(1);
        person.setName("Lusa");
    }
}
